/**********************************************************************************
**
** Copyright (C) 2009 Jostein Bratlie
** Contact: jostein.bratlie@gmail.com | http://bratlie.info | http://episteme.hin.no
**
** This file is part of GMQtExt, a GMlib and Qt extension library
**
** GMQtExt is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** GMQtExt is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with GMQtExt.  If not, see <http://www.gnu.org/licenses/>.
**
**********************************************************************************/

#include <gmOpenGL>

// Qt
#include <QtOpenGL/QGLWidget>
#include <QtCore/QDebug>

// header
#include "scenepropertybrowser.h"

// local
#include "../glwidget/glwidget.h"

// GMlib
#include <gmWindow>


namespace GMQtExt {


  ScenePropertyBrowser::ScenePropertyBrowser( QWidget* parent ) : PropertyBrowser( parent ) {

    _scene = 0x0;
  }

  void ScenePropertyBrowser::beginSetup() {

    PropertyBrowser::beginSetup();

    _scene = 0x0;
  }

  void ScenePropertyBrowser::set( GMlib::Scene *s ) {

    beginSetup();

    GMlib::GMWindow *w = dynamic_cast<GMlib::GMWindow*>( s );

    if( !w )
      return;

    _scene = s;

    QtProperty *scene;
    scene = createGroup( "Scene", "scene", getBrowser(), true );
    setBackgroundColor( scene, QColor("LightYellow") );

    QtProperty *info;
    info = createGroup( "Info", "scene_info", scene, true );

    QtProperty *scene_objects;
    scene_objects = createGroup( "Scene Objects", "scene_info_scene_objects", info, true );

    createVariantLabel( QVariant::String, "Objects", "N/A", "scene_info_other_objects", scene_objects );
    createVariantLabel( QVariant::String, "Cameras", QString("%1").arg(s->getLights().getSize()),
                        "scene_info_cameras", scene_objects );
    createVariantLabel( QVariant::String, "Lights", "N/A", "scene_info_lights", scene_objects );

    QtProperty *surroundingsphere;
    surroundingsphere = createGroup( "Surrounding Sphere", "scene_info_surroundingsphere", info, true );


    createVariantLabel( QVariant::String, "Clean", QString("%1").arg( s->getSphereClean().getRadius() ),
                        "scene_info_surroundingsphere_clean", surroundingsphere );

    createVariantLabel( QVariant::String, "Total", QString("%1").arg( s->getSphere().getRadius() ),
                        "scene_info_surroundingsphere_total", surroundingsphere );

    QtProperty *settings;
    settings = createGroup( "Settings", "scene_settings", scene, true );

    createVariantProperty( QVariant::Int, "Stereo", w->isStereoEnabled(),
                           "scene_settings_stereo", settings );
    createVariantProperty( QVariant::Int, "Running", w->isRunning(),
                           "scene_settings_running", settings );

    GLfloat cc[4];
    glGetFloatv( GL_COLOR_CLEAR_VALUE, cc );

    QColor c;
    c.setRedF( cc[0] );
    c.setGreenF( cc[1] );
    c.setBlueF( cc[2] );
    c.setAlphaF( cc[3] );

    createVariantProperty( QVariant::Color, "Clear Color", c, "scene_settings_clearcolor", settings );

    endSetup();
  }

  void ScenePropertyBrowser::slotVariantPropertyChanged( QtProperty* property, const QVariant& value ) {

    if( !_scene )
      return;

    GMlib::GMWindow *w = dynamic_cast<GMlib::GMWindow*>( _scene );

    if(!w)
      return;

    QVariant id = getPropertyId(property);

    if( id == "scene_settings_stereo" ) {

      if( w->isStereoEnabled() != value.toBool() )
        w->toggleStereo();
    }
    else if( id == "scene_settings_running" ) {

      if( w->isRunning() != value.toBool() )
        w->toggleRun();
    }
    else if( id == "scene_settings_clearcolor" ) {

      qDebug() << "scene_settings_clearcolor";

      GLWidget *widget = dynamic_cast<GLWidget*>( _scene );
      if( widget )
        qDebug() << "Widget is known";

      QColor c = value.value<QColor>();
      widget->qglClearColor( c );
    }
  }

} // END namespace GMQtExt
